#!/usr/bin/env python

# Iroad sim - running lights algorith simulator
# Copyright (C) 2010 Laurynas Riliskis <laurynas.riliskis@gmail.com>
#
# This file is part of IroadSim.
#
# IroadSim is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# IroadSimis distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with IroadSim.  If not, see <http://www.gnu.org/licenses/>

__author__ = "Laurynas Riliskis <laurynas.riliskis@gmail.com>"
__version__ = "0.1"
__date__ = "Date: 2010/09/01"
__copyright__ = "Copyright (c) 2007 Laurynas Riliskis"
__license__ = "GPL"

try:
	from pyglet import window,  clock, font
except ImportError:
	print "You need to install pyglet"
	print "http://www.pyglet.org/"
	exit()

from math import sin , cos , pi

from nodeM import *

import helper



#main game window
class RunningLightWindow(window.Window):
	def __init__(self, *args, **kwargs):
		#Let all of the standard stuff pass through
		window.Window.__init__(self, *args, **kwargs)
		self.set_mouse_visible(True)
		self.init_sprites()

	def init_sprites(self):
		self.x_center = (win_width-20)/2
		self.y_center = (win_height-20)/2
		self.r=self.x_center-20
		self.angle=(2*pi)/NUMBER_OF_NODES
		self.nodes =[]
		self.create_nodes()

	#calculate X position on the circle
	def getX(self, num):
		return self.x_center+self.r*sin(num*self.angle)-10
	#calculate Y position on the circle
	def getY(self, num):
		return self.y_center+self.r*cos(num*self.angle) -10
		
	def main_loop(self):
		#Create a font for our FPS clock
		ft = font.load('Arial', 28)
		#The pyglet.font.Text object to display the FPS		
		fps_text = font.Text(ft, y=10)
		
		#HINT: make sure that the fps and 
		#your node timer settings are in sync ;)
		#clock.set_fps_limit(60)

		while not self.has_exit:
			self.dispatch_events()
			self.clear()
			self.update()
			self.draw()
			#Tick the clock
			clock.tick()
			#Gets fps and draw it
			fps_text.text = ("fps: %d") % (clock.get_fps())
			fps_text.draw()
			self.flip()

	def update(self):
		for n in self.nodes:
			n.update()


	def draw(self):
		for n in self.nodes:
			n.draw()

	#make nodes
	def create_nodes(self):
		for i in range(1,NUMBER_OF_NODES+1):
			self.nodes.append(Node(i, x=self.getX(i) , y=self.getY(i)))



class NodeImage(object):
	def __init__(self, **kwargs):
		#init standard variables
		self.image_on = helper.load_image("led_on.png")
		self.image_off = helper.load_image("led_off.png")
		self.currImg=self.image_off
		#Update the dict if they sent in any keywords
		#here are x and y updated
		self.__dict__.update(kwargs)		

	def draw(self):
		self.currImg.blit(self.x, self.y)

	def update(self):
		if self.ledState():
			self.currImg=self.image_on
		else:
			self.currImg=self.image_off



       
#start application
#python iroad.py
if __name__ == "__main__":
	light = RunningLightWindow(win_width,win_height)
	light.main_loop()

